package asia.yuanding.wechat.web.netty.handler.impl;

import asia.yuanding.wechat.web.netty.handler.annotation.MessageProcessor;
import asia.yuanding.wechat.web.netty.handler.annotation.NettyMessageHandler;
import cn.hutool.json.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户相关消息处理器
 * 
 * @author Kiro
 */
@Slf4j
@NettyMessageHandler("user")
public class UserMessageHandler {

    @MessageProcessor(value = "login", priority = 0)
    public String userLogin(JSONObject reqEntity, ChannelHandlerContext ctx) {
        String userId = reqEntity.getStr("userId");
        String token = reqEntity.getStr("token");
        
        log.info("用户登录: userId={}, token={}", userId, token);
        
        // 将用户ID绑定到Channel
        ctx.channel().attr(AttributeKey.valueOf("uId")).set(userId);
        
        JSONObject response = new JSONObject();
        response.set("type", "user");
        response.set("method", "loginResult");
        response.set("success", true);
        response.set("userId", userId);
        response.set("message", "登录成功");
        
        return response.toString();
    }

    @MessageProcessor("logout")
    public String userLogout(JSONObject reqEntity, ChannelHandlerContext ctx) {
        String userId = getUserId(ctx);
        
        log.info("用户退出登录: userId={}", userId);
        
        // 清除用户ID
        ctx.channel().attr(AttributeKey.valueOf("uId")).set(null);
        
        JSONObject response = new JSONObject();
        response.set("type", "user");
        response.set("method", "logoutResult");
        response.set("success", true);
        response.set("message", "退出成功");
        
        return response.toString();
    }

    @MessageProcessor("profile")
    public String getUserProfile(JSONObject reqEntity, ChannelHandlerContext ctx) {
        String targetUserId = reqEntity.getStr("targetUserId");
        String currentUserId = getUserId(ctx);
        
        log.info("用户 {} 请求用户 {} 的资料", currentUserId, targetUserId);
        
        JSONObject response = new JSONObject();
        response.set("type", "user");
        response.set("method", "profile");
        response.set("userId", targetUserId);
        response.set("nickname", "用户" + targetUserId);
        response.set("avatar", "default_avatar.jpg");
        response.set("status", "online");
        
        return response.toString();
    }

    @MessageProcessor("status")
    public String updateStatus(JSONObject reqEntity, ChannelHandlerContext ctx) {
        String status = reqEntity.getStr("status");
        String userId = getUserId(ctx);
        
        log.info("用户 {} 更新状态: {}", userId, status);
        
        JSONObject response = new JSONObject();
        response.set("type", "user");
        response.set("method", "statusResult");
        response.set("success", true);
        response.set("status", status);
        
        return response.toString();
    }

    private String getUserId(ChannelHandlerContext ctx) {
        Object userId = ctx.channel().attr(AttributeKey.valueOf("uId")).get();
        return userId != null ? userId.toString() : "unknown";
    }
}